package com.ruoyi.web.controller.system;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.quartz.SchedulerException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.GaeaReportDataSource;
import com.ruoyi.system.service.IGaeaReportDataSourceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 数据源管理Controller
 * 
 * @author ruoyi
 * @date 2022-05-01
 */
@Api(value = "数据源管理Controller",tags = "数据源管理Controller")

@RestController
@RequestMapping("/report/datasource")
public class GaeaReportDataSourceController extends BaseController
{
    @Autowired
    private IGaeaReportDataSourceService gaeaReportDataSourceService;

    /**
     * 查询数据源管理列表
     */
    @ApiOperation("查询数据源管理列表")
    @PreAuthorize("@ss.hasPermi('report:datasource:list')")
    @GetMapping("/list")
    public TableDataInfo list(GaeaReportDataSource gaeaReportDataSource)
    {
//        GaeaReportDataSource a=new GaeaReportDataSource();
//        a.setEnableFlag(1);
//        a.setId(2L);
//        a.setAccessKey("bf7e108e0631a9353ca59794e3f82bf0");
//        a.setCaseResult("[{\\\"time\\\":\\\"2021-08-23\\\",\\\"type\\\":\\\"成功\\\",\\\"nums\\\":12},{\\\"time\\\":\\\"2021-08-23\\\",\\\"type\\\":\\\"失败\\\",\\\"nums\\\":1},{\\\"time\\\":\\\"2021-08-24\\\",\\\"type\\\":\\\"成功\\\",\\\"nums\\\":24},{\\\"time\\\":\\\"2021-08-24\\\",\\\"type\\\":\\\"失败\\\",\\\"nums\\\":5},{\\\"time\\\":\\\"2021-08-25\\\",\\\"type\\\":\\\"成功\\\",\\\"nums\\\":13},{\\\"time\\\":\\\"2021-08-25\\\",\\\"type\\\":\\\"失败\\\",\\\"nums\\\":8},{\\\"time\\\":\\\"2021-08-26\\\",\\\"type\\\":\\\"成功\\\",\\\"nums\\\":19},{\\\"time\\\":\\\"2021-08-26\\\",\\\"type\\\":\\\"失败\\\",\\\"nums\\\":3},{\\\"time\\\":\\\"2021-08-27\\\",\\\"type\\\":\\\"成功\\\",\\\"nums\\\":9},{\\\"time\\\":\\\"2021-08-27\\\",\\\"type\\\":\\\"失败\\\",\\\"nums\\\":15}]");
//        a.setCreateBy("admin");
//        a.setDynSentence("SELECT time,type,nums from aj_report_comparestack");
//        a.setSetCode("compare_ajreport");
//        a.setSetName("setName");
//        a.setSetType("setType");
//        a.setSourceCode("sourceCode");
//        a.setUpdateBy("admin");
        startPage();
        List<GaeaReportDataSource> list = gaeaReportDataSourceService.selectGaeaReportDataSourceList(gaeaReportDataSource);
//        List<GaeaReportDataSource> newlist= new LinkedList<GaeaReportDataSource>();
//        newlist.add(a);
//        list.add(a);
        return getDataTable(list);
    }

    /**
     * 导出数据源管理列表
     */
    @ApiOperation("导出数据源管理列表")
    @PreAuthorize("@ss.hasPermi('report:datasource:export')")
    @Log(title = "数据源管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GaeaReportDataSource gaeaReportDataSource)
    {
        List<GaeaReportDataSource> list = gaeaReportDataSourceService.selectGaeaReportDataSourceList(gaeaReportDataSource);
        ExcelUtil<GaeaReportDataSource> util = new ExcelUtil<GaeaReportDataSource>(GaeaReportDataSource.class);
        util.exportExcel(response, list, "数据源管理数据");
    }

    /**
     * 获取数据源管理详细信息
     */
    @ApiOperation("获取数据源管理详细信息")
    @PreAuthorize("@ss.hasPermi('report:datasource:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(gaeaReportDataSourceService.selectGaeaReportDataSourceById(id));
    }

    /**
     * 新增数据源管理
     */
    @ApiOperation("新增数据源管理")
    @PreAuthorize("@ss.hasPermi('report:datasource:add')")
    @Log(title = "数据源管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GaeaReportDataSource gaeaReportDataSource)
    {
        return toAjax(gaeaReportDataSourceService.insertGaeaReportDataSource(gaeaReportDataSource));
    }

    /**
     * 修改数据源管理
     */
    @ApiOperation("修改数据源管理")
    @PreAuthorize("@ss.hasPermi('report:datasource:edit')")
    @Log(title = "数据源管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GaeaReportDataSource gaeaReportDataSource)throws SchedulerException
    {
        return toAjax(gaeaReportDataSourceService.updateGaeaReportDataSource(gaeaReportDataSource));
    }

    /**
     * 删除数据源管理
     */
    @ApiOperation("删除数据源管理")
    @PreAuthorize("@ss.hasPermi('report:datasource:remove')")
    @Log(title = "数据源管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(gaeaReportDataSourceService.deleteGaeaReportDataSourceByIds(ids));
    }

    /**
     * 数据源状态修改
     */
    @ApiOperation("数据源状态修改")
    @PreAuthorize("@ss.hasPermi('report:datasource:changeStatus')")
    @Log(title = "数据源状态修改", businessType = BusinessType.UPDATE)
    @PostMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody GaeaReportDataSource gaeaReportDataSource )
    {

//        long b = (long)id;
        GaeaReportDataSource newDataSource=gaeaReportDataSourceService.selectGaeaReportDataSourceById(gaeaReportDataSource.getId());
        newDataSource.setEnableFlag(gaeaReportDataSource.getEnableFlag());
        return toAjax(gaeaReportDataSourceService.updateGaeaReportDataSource(newDataSource));
    }




}
